home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 366_01 / ue311c1.arc / BASIC.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  16KB  |  617 lines

  1. /*    basic.c:    Basic movement functions for
  2.  *            MicroEMACS
  3.  *            (C)Copyright 1990 by Daniel Lawrence
  4.  *
  5.  * The routines in this file move the cursor around on the screen. They
  6.  * compute a new value for the cursor, then adjust ".". The display code
  7.  * always updates the cursor location, so only moves between lines, or
  8.  * functions that adjust the top line in the window and invalidate the
  9.  * framing, are hard.
  10.  */
  11. #include    <stdio.h>
  12. #include    "estruct.h"
  13. #include    "eproto.h"
  14. #include    "edef.h"
  15. #include    "elang.h"
  16.  
  17. /*
  18.  * Move the cursor to the
  19.  * beginning of the current line.
  20.  * Trivial.
  21.  */
  22. PASCAL NEAR gotobol(f, n)
  23.  
  24. int f,n;    /* argument falg and num */
  25.  
  26. {
  27.         curwp->w_doto  = 0;
  28.         return(TRUE);
  29. }
  30.  
  31. /*
  32.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  33.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  34.  * location. Error if you try and move out of the buffer. Set the flag if the
  35.  * line pointer for dot changes.
  36.  */
  37. PASCAL NEAR backchar(f, n)
  38.  
  39. int f,n;    /* prefix flag and argument */
  40.  
  41. {
  42.         register LINE   *lp;
  43.  
  44.         if (n < 0)
  45.                 return(forwchar(f, -n));
  46.         while (n--) {
  47.                 if (curwp->w_doto == 0) {
  48.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  49.                                 return(FALSE);
  50.                         curwp->w_dotp  = lp;
  51.                         curwp->w_doto  = llength(lp);
  52.                         curwp->w_flag |= WFMOVE;
  53.                 } else
  54.                         curwp->w_doto--;
  55.         }
  56. #if    DBCS
  57.     return(stopback());
  58. #else
  59.         return(TRUE);
  60. #endif
  61. }
  62.  
  63. /*
  64.  * Move the cursor to the end of the current line. Trivial. No errors.
  65.  */
  66. PASCAL NEAR gotoeol(f, n)
  67.  
  68. int f,n;    /* argument falg and num */
  69.  
  70. {
  71.         curwp->w_doto  = llength(curwp->w_dotp);
  72.         return(TRUE);
  73. }
  74.  
  75. /*
  76.  * Move the cursor forwards by "n" characters. If "n" is less than zero call
  77.  * "backchar" to actually do the move. Otherwise compute the new cursor
  78.  * location, and move ".". Error if you try and move off the end of the
  79.  * buffer. Set the flag if the line pointer for dot changes.
  80.  */
  81. PASCAL NEAR forwchar(f, n)
  82.  
  83. int f,n;    /* prefix flag and argument */
  84.  
  85. {
  86.         if (n < 0)
  87.                 return(backchar(f, -n));
  88.         while (n--) {
  89.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  90.                         if (curwp->w_dotp == curbp->b_linep)
  91.                                 return(FALSE);
  92.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  93.                         curwp->w_doto  = 0;
  94.                         curwp->w_flag |= WFMOVE;
  95.                 } else
  96.                         curwp->w_doto++;
  97.         }
  98. #if    DBCS
  99.     return(stopforw());
  100. #else
  101.         return(TRUE);
  102. #endif
  103. }
  104.  
  105. PASCAL NEAR gotoline(f, n)    /* move to a particular line.
  106.                argument (n) must be a positive integer for
  107.                this to actually do anything        */
  108.  
  109. int f,n;    /* prefix flag and argument */
  110.  
  111. {
  112.     register int status;    /* status return */
  113.     char arg[NSTRING];    /* buffer to hold argument */
  114.  
  115.     /* get an argument if one doesnt exist */
  116.     if (f == FALSE) {
  117.         if ((status = mlreply(TEXT7, arg, NSTRING)) != TRUE) {
  118. /*                                    "Line to GOTO: " */
  119.             mlwrite(TEXT8);
  120. /*                              "[Aborted]" */
  121.             return(status);
  122.         }
  123.         n = asc_int(arg);
  124.     }
  125.  
  126.     if (n < 1)        /* if a bogus argument...then leave */
  127.         return(FALSE);
  128.  
  129.     /* first, we go to the start of the buffer */
  130.         curwp->w_dotp  = lforw(curbp->b_linep);
  131.         curwp->w_doto  = 0;
  132.     return(forwline(f, n-1));
  133. }
  134.  
  135. /*
  136.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  137.  * considered to be hard motion; it really isn't if the original value of dot
  138.  * is the same as the new value of dot. Normally bound to "M-<".
  139.  */
  140. PASCAL NEAR gotobob(f, n)
  141.  
  142. int f,n;    /* argument flag and num */
  143.  
  144. {
  145.         curwp->w_dotp  = lforw(curbp->b_linep);
  146.         curwp->w_doto  = 0;
  147.         curwp->w_flag |= WFMOVE;
  148.         return(TRUE);
  149. }
  150.  
  151. /*
  152.  * Move to the end of the buffer. Dot is always put at the end of the file
  153.  * (ZJ). The standard screen code does most of the hard parts of update.
  154.  * Bound to "M->".
  155.  */
  156. PASCAL NEAR gotoeob(f, n)
  157.  
  158. int f,n;    /* argument falg and num */
  159.  
  160. {
  161.         curwp->w_dotp  = curbp->b_linep;
  162.         curwp->w_doto  = 0;
  163.         curwp->w_flag |= WFMOVE;
  164.         return(TRUE);
  165. }
  166.  
  167. /*
  168.  * Move forward by full lines. If the number of lines to move is less than
  169.  * zero, call the backward line function to actually do it. The last command
  170.  * controls how the goal column is set. Bound to "C-N". No errors are
  171.  * possible.
  172.  */
  173. PASCAL NEAR forwline(f, n)
  174.  
  175. int f,n;    /* argument falg and num */
  176.  
  177. {
  178.         register LINE   *dlp;
  179.  
  180.         if (n < 0)
  181.                 return(backline(f, -n));
  182.  
  183.     /* if we are on the last line as we start....fail the command */
  184.     if (curwp->w_dotp == curbp->b_linep)
  185.         return(FALSE);
  186.  
  187.     /* if the last command was not note a line move,
  188.        reset the goal column */
  189.         if ((lastflag&CFCPCN) == 0)
  190.                 curgoal = getccol(FALSE);
  191.  
  192.     /* flag this command as a line move */
  193.         thisflag |= CFCPCN;
  194.  
  195.     /* and move the point down */
  196.         dlp = curwp->w_dotp;
  197.         while (n-- && dlp!=curbp->b_linep)
  198.                 dlp = lforw(dlp);
  199.  
  200.     /* reseting the current position */
  201.         curwp->w_dotp  = dlp;
  202.         curwp->w_doto  = getgoal(dlp);
  203.         curwp->w_flag |= WFMOVE;
  204. #if    DBCS
  205.     return(stopback());
  206. #else
  207.         return(TRUE);
  208. #endif
  209. }
  210.  
  211. /*
  212.  * This function is like "forwline", but goes backwards. The scheme is exactly
  213.  * the same. Check for arguments that are less than zero and call your
  214.  * alternate. Figure out the new line and call "movedot" to perform the
  215.  * motion. No errors are possible. Bound to "C-P".
  216.  */
  217. PASCAL NEAR backline(f, n)
  218.  
  219. int f,n;    /* argument falg and num */
  220.  
  221. {
  222.         register LINE   *dlp;
  223.  
  224.         if (n < 0)
  225.                 return(forwline(f, -n));
  226.  
  227.  
  228.     /* if we are on the last line as we start....fail the command */
  229.     if (lback(curwp->w_dotp) == curbp->b_linep)
  230.         return(FALSE);
  231.  
  232.     /* if the last command was not note a line move,
  233.        reset the goal column */
  234.         if ((lastflag&CFCPCN) == 0)
  235.                 curgoal = getccol(FALSE);
  236.  
  237.     /* flag this command as a line move */
  238.         thisflag |= CFCPCN;
  239.  
  240.     /* and move the point up */
  241.         dlp = curwp->w_dotp;
  242.         while (n-- && lback(dlp)!=curbp->b_linep)
  243.                 dlp = lback(dlp);
  244.  
  245.     /* reseting the current position */
  246.         curwp->w_dotp  = dlp;
  247.         curwp->w_doto  = getgoal(dlp);
  248.         curwp->w_flag |= WFMOVE;
  249. #if    DBCS
  250.     return(stopback());
  251. #else
  252.         return(TRUE);
  253. #endif
  254. }
  255.  
  256. PASCAL NEAR gotobop(f, n) /* go back to the beginning of the current paragraph
  257.            here we look for a blank line or a character from
  258.            $paralead to delimit the beginning of a paragraph or
  259.            $fmtlead to delimit a line before the paragraph */
  260.  
  261. int f, n;    /* default Flag & Numeric argument */
  262.  
  263. {
  264.     register int suc;    /* success of last backchar */
  265.     register int c;        /* current character in scan */
  266.     register char *sp;    /* ptr into character leadin lists */
  267.  
  268.     if (n < 0)    /* the other way...*/
  269.         return(gotoeop(f, -n));
  270.  
  271.     while (n-- > 0) {    /* for each one asked for */
  272.  
  273.         /* first scan back until we are in a word */
  274.         suc = backchar(FALSE, 1);
  275.         while (!inword() && suc)
  276.             suc = backchar(FALSE, 1);
  277.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  278.  
  279.         /* scan back through the text */
  280.         while (lback(curwp->w_dotp) != curbp->b_linep) {
  281.  
  282.             /* at blank line */
  283.             if (llength(curwp->w_dotp) == 0)
  284.                 break;
  285.  
  286.             /* current line start with member of $paralead? */
  287.             c = lgetc(curwp->w_dotp, 0);
  288.             sp = paralead;
  289.             while (*sp) {
  290.                 if (c == *sp)
  291.                     break;
  292.                 ++sp;
  293.             }
  294.             if (c == *sp)
  295.                 break;            
  296.  
  297.             /* last line start with member of $fmtlead? */
  298.             c = lgetc(lback(curwp->w_dotp), 0);
  299.             sp = fmtlead;
  300.             while (*sp) {
  301.                 if (c == *sp)
  302.                     break;
  303.                 ++sp;
  304.             }
  305.             if (c == *sp)
  306.                 break;            
  307.  
  308.